﻿/*
 *Copyright(C) 2020 by  GYYX All rights reserved.
 *Unity版本：2018.4.23f1 
 *作者:程一峰  
 *创建日期: 2021-04-08 
 *模块说明：
 *版本: 1.2
*/


using UnityEngine;
using Warfare.FSM;

namespace WarfareEditor.FSM
{
    /// <summary>
    /// 右侧的设计面板；
    /// </summary>
    public class FSMZoom_Design : FSMZoom
    {
        private FSMZoom_DesignMouseHandler mouseHandler;
        private FSMZoom_Line lineHandler;

        protected internal override void Init(FSMEditorWindow aWindow)
        {
            base.Init(aWindow);
            mouseHandler = new FSMZoom_DesignMouseHandler();
            mouseHandler.Init(aWindow);

            lineHandler = new FSMZoom_Line();
            lineHandler.Init(aWindow);
        }

        protected internal override void OnDestroy()
        {
            mouseHandler.OnDestroy();
            lineHandler.OnDestroy();
        }

        private Vector3 scrollPos = Vector2.zero;
        private Rect scrollRect = new Rect(0, 0, 1500, 1000);
        private Rect contentRect = new Rect(0, 0, 3000, 2000);

        protected internal override void OnGUI()
        {
            scrollPos = GUI.BeginScrollView(scrollRect, scrollPos, contentRect);
            mouseHandler.OnGUI();
            window.DrawWindow(Do_DrawZoom);
            GUI.EndScrollView();
        }

        private void Do_DrawZoom()
        {
            if (window.CurrentAsset == null)
                return;
            //先画根节点；
            DrawNodeMethd(window.CurrentAsset.RootNode, 0);
            //再画后续节点；
            var L = window.CurrentAsset.ListNodes;
            var length = L.Count;
            for (int i = 0; i < length; i++)
            {
                var node = L[i];
                DrawNodeMethd(node, i + 1);
            }
            lineHandler.OnGUI();
        }

        private void DrawNodeMethd(FSMNode node, int index)
        {
            var nodeRect = node.NodeRect;
            GUIContent content = new GUIContent(node.NodeName);
            Rect rect = GUI.Window(index, nodeRect, DrawNodeWindow, content);
        }

        private void DrawNodeWindow(int id)
        {
            if (id == 0)
            {
                //根节点；
                FSMEditorTool.DrawNode(window.CurrentAsset.RootNode);
            }
            else
            {
                var L = window.CurrentAsset.ListNodes;
                //这里要把Index -1 因为根节点占用了一个；
                FSMEditorTool.DrawNode(L[id - 1]);
            }
            GUI.DragWindow();
        }

    }
}